package org.oimwrapper.connectors;

import com.thortech.xl.vo.*;
import java.util.*;

/**
 * Represents the parent and child form data associated with an object.
 */
public class OIMFormData {

	/**
	 * The parent form data.
	 */
	protected Map parentData;
	
	/**
	 * The chidl data - tables names and arrays of child records.
	 */
	protected Map childData;
	
	/**
	 * Default Constructor.
	 */
	public OIMFormData() {
		parentData = new HashMap();
		childData = new HashMap();
	}
	
	/**
	 * Loads form data from an existing value set.
	 */
	public OIMFormData(UDFormData Data) {
		parentData = Data.getFormData();
		childData = new HashMap();
		for (Iterator iterator = Data.getChildTables().entrySet().iterator(); iterator.hasNext(); ) {
			String childTable = iterator.next().toString();
			UDChildTableRecord[] records = Data.getChildTableRecords(childTable);
			List recordList = new ArrayList();
			for (int i = 0; i < records.length; i++) {
				recordList.add(records[i].getRecordData());
			}
			childData.put(childTable.toUpperCase(), recordList.toArray(new Map[0]));
		}
	}
	
	/**
	 * Sets the parent form values.
	 * 
	 * @param Data The parent form values.
	 */
	public void setParentData(Map Data) {
		parentData = Data;
	}
	
	/**
	 * Sets the child records for a particular child table.
	 * 
	 * @param ChildTable The name of the child table.
	 * @param Records The records to set for the child table.
	 */
	public void setChildData(String ChildTable, Map[] Records) {
		childData.put(ChildTable.toUpperCase(), Records);
	}
	
	/**
	 * Gets the parent data.
	 * 
	 * @return The parent data.
	 */
	public Map getParentData() {
		return parentData;
	}
	
	/**
	 * Gets the child tables.
	 * 
	 * @return The child tables.
	 */
	public String[] getChildTables() {
		return (String[]) childData.keySet().toArray(new String[0]);
	}
	
	/**
	 * Gets the child data.
	 * 
	 * @param ChildTable The name of the child table to get the data for.
	 * @return The child data for a specific table.
	 */
	public Map[] getChildData(String ChildTable) {
		return (Map[]) childData.get(ChildTable.toUpperCase());
	}
}